package com.dm.cloud.core.exception;

import com.dm.cloud.core.mode.ResultModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
@Slf4j
public class OauthErrorAdvice {

    @ExceptionHandler(value = {CustomAuthException.class})
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ResponseBody
    public ResponseEntity<ResultModel> authException(HttpServletRequest request,
                                        HttpServletResponse response,
                                        CustomAuthException exception) {
        exception.printStackTrace();
        return ResponseEntity.status(403)
                .body(ResultModel.err(exception.getCode(),exception.getMessage()));
    }
}
